<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class verify_login_member extends CI_Controller {
    function __construct() {
        parent::__construct();
        //load session and connect to database
        $this->load->helper('bara_helper');
        $this->load->library('bara_lib');
        $this->load->model('modul/login_member_model','login',TRUE);
        $this->load->helper(array('form', 'url','html'));
        $this->load->library(array('form_validation','session'));

        $this->load->model('modul/model_menu');

        $this->load->model('modul/about_model');
        $this->load->model('modul/article_model');
        $this->load->model('modul/sosmed_model');
    }
 
    function index() {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
 
        if($this->form_validation->run() == FALSE) {

            $data['text_home'] = "Home";
            $data['text_title'] = "Login";

            $this->load->helper(array('form','html'));

            $data['content'] = 'frontend/'. $this->bara_lib->theme() .'/modul/member/login_member';
            
            //menu
            $data['get_menus'] = $this->model_menu->get_menu($this->bara_lib->language());

            // sosmed
            $data['gets_facebook'] = $this->sosmed_model->get_sosmed('facebook');
            $data['gets_twitter'] = $this->sosmed_model->get_sosmed('twitter');
            $data['gets_sosmed'] = $this->sosmed_model->get_sosmed_all();

            //footer
            $data['gets_about_footer'] = $this->about_model->get_about('about_us',$this->bara_lib->language());
            $data['get_articles_footer'] = $this->article_model->get_article($this->bara_lib->language(),3,0);
            //end footer

            $data['head'] = 'layout/'. $this->bara_lib->theme() .'/head';
            $data['menu'] = 'layout/'. $this->bara_lib->theme() .'/menu';
            $data['footer'] = 'layout/'. $this->bara_lib->theme() .'/footer';
            $this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data);   
            } else {
                //Go to private area
                redirect(base_url('modul/halaman_member'), 'refresh');
            }       
     }
 
     function check_database($password) {
         //Field validation succeeded.  Validate against database
         $username = $this->input->post('username');
         //query the database
         $result = $this->login->login($username, $password);
         if($result) {
             $sess_array = array();
             foreach($result as $row) {
                 //create the session
                 $sess_array = array('id_member' => $row->id_member,
                     'username_member' => $row->username_member);
                 //set session with value from database
                 $this->session->set_userdata('logged_in', $sess_array);
                 }
          return TRUE;
          } else {
              //if form validate false
              $this->form_validation->set_message('check_database', 'Invalid username or password');
              return FALSE;
          }
      }
}